<!DOCTYPE html>
<html>
<head>
    <title>地址列表</title>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <link rel="stylesheet" href="../lib/weui.min.css">
    <link rel="stylesheet" href="../css/jquery-weui.css">
    <link rel="stylesheet" href="../css/base.css">
    <link rel="stylesheet" href="../css/address.css">
</head>
<body>
<div class="xyg-address weui-cells" id="addressList">
    <script type="text/x-handlebars-template" id="address-list-temp">
        {{#each list}}
        <a class="weui-cell weui-cell_access">
            <div class="weui-cell__bd">
                <div class="xyg-add-detail-text" data-id="{{id}}" onclick="goOrderDetail(this)">
                    {{name}}&nbsp;&nbsp;<span>{{phone}}</span>
                </div>
                <div class="xyg-add-detail-desc">
                    {{province}}{{descripts}}
                </div>
            </div>
        </a>
        <div class="xyg-address-btn weui-cells">
            <div class="xyg-address-btn-left">
                <input name="weuiAgree" type="checkbox" disabled {{getDefaultType type}} class="weui-agree__checkbox">
                <span class="weui-agree__text">
                默认地址
                </span>
            </div>
            <div class="xyg-address-btn-right">
                <div onclick="delAddress(this)" data-id="{{id}}">
                    <img src="../images/icon-del-address.png"/>
                </div>
                <div onclick="editAddress(this)" data-id="{{id}}">
                    <img src="../images/icon-editer-address.png"/>
                </div>
            </div>
        </div>
        {{/each}}
    </script>
</div>
<div style="width:100%; height:2.5rem;"></div>
<a class="xyg-btn-add-address weui-btn weui-btn_primary" onclick="addAddress()">新建地址</a>
</body>
<script src="../lib/jquery-2.1.4.js"></script>
<script src="../js/jquery-weui.js"></script>
<script src="../fonts/iconfont.js"></script>
<script src="../lib/handlebars.min.js"></script>
<script src="../js/config.js"></script>
<script>
    var memberId = getQueryString("memberId");
    var orderId = getQueryString("orderId");
    var orderItemJson = getQueryString("orderItemJson");
    var type = getQueryString("type");

    // 获取当前用户地址列表
    $.get(baseURL + "api/address/list/"+memberId,"",function(res) {
        if (res.code == 0) {
            //var address = res.address;
            var addressTemp = $('#address-list-temp').html();
            var hanAddress = Handlebars.compile(addressTemp);
            $('#addressList').html(hanAddress(res));
        }
    });

    function addAddress() {
        if (type == "1") {
            window.location.href="/mobile/view/addAddress.html?memberId="+memberId+"&type="+type;
        }
        if (type == "2"){
            window.location.href="/mobile/view/addAddress.html?memberId="+memberId+"&type="+type+
                "&orderItemJson="+encodeURI(orderItemJson);
        }
        if (type == "3"){
            window.location.href="/mobile/view/addAddress.html?memberId="+memberId+"&type="+type+
                "&orderId="+orderId;
        }
    }

    function editAddress(obj){
        var addressId = $(obj).attr("data-id");
        // 正常
        if (type == "1") {
            window.location.href="/mobile/view/addAddress.html?memberId="+memberId+"&type="+type+
                "&addressId="+addressId;
        }
        // 临时订单
        if (type == "2"){
            window.location.href="/mobile/view/addAddress.html?memberId="+memberId+"&type="+type+
                "&orderItemJson="+encodeURI(orderItemJson)+"&addressId="+addressId;
        }
        // 已生成订单
        if (type == "3"){
            window.location.href="/mobile/view/addAddress.html?memberId="+memberId+"&type="+type+
                "&orderId="+orderId+"&addressId="+addressId;
        }
    }

    function goOrderDetail(obj) {
        var addressId = $(obj).attr("data-id");
        if (type == "2"){
            window.location.href="/mobile/view/addDetail.html?memberId="+memberId+"&type="+type+
                "&orderItemJson="+encodeURI(orderItemJson)+"&addressId="+addressId;
        }
        if (type == "3"){
            window.location.href="/mobile/view/addDetail.html?memberId="+memberId+"&type="+type+
                "&orderId="+orderId+"&addressId="+addressId;
        }
    }

    function delAddress(obj) {
        var addressId = $(obj).attr("data-id");
        $.confirm("您确定要删除该地址吗？", "删除提示", function () {
            $.showLoading("正在提交中");
            $.ajax({
                type: "POST",
                url: baseURL + "api/address/delete/" + addressId +"?_method=delete",
                success: function (res) {
                    if (res.code == 0) {
                        $.hideLoading();
                        $.toast("删除成功", "text");
                        // 刷新当前页面
                        goAddressAndType();
                    } else {
                        $.hideLoading();
                        $.toast(res.msg, "text");
                    }
                },
                error: function (result) {
                    $.hideLoading();
                    $.toast(result, "text");
                }
            });
        }, function () {
            //取消操作
        });
    }

    //memberId, type, orderId, orderItemJson
    function goAddressAndType(){
        // 正常
        if (type == "1") {
            window.location.href="/mobile/view/address.html?memberId="+memberId+"&type="+type;
        }
        // 临时订单
        if (type == "2"){
            window.location.href="/mobile/view/address.html?memberId="+memberId+"&type="+type+
                "&orderItemJson="+encodeURI(orderItemJson);
        }
        // 已生成订单
        if (type == "3"){
            window.location.href="/mobile/view/address.html?memberId="+memberId+"&type="+type+
                "&orderId="+orderId;
        }
    }

    Handlebars.registerHelper("getDefaultType",function(str) {
        if(str != null && str == "2"){
            return "checked"
        }
        return "";
    });
</script>
</html>

